package com.example.inoculations.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.inoculations.entity.Address;
import com.example.inoculations.resultR.R;
import com.example.inoculations.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author meirui
 * @since 2023-03-04
 */
@RestController
@RequestMapping("/inoculations/address")
public class AddressController {
    @Autowired
    IAddressService iAddressService;

    /*分页查询*/
    @GetMapping("/pageall")
    public R<IPage<Address>> getpageall(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam(required = false) String theaddress,
                                       @RequestParam(required = false) String fromhospital) {
        IPage<Address> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        /*根据地址模糊查询*/
        if (!StringUtils.isEmpty(theaddress)){
            queryWrapper.like("address",theaddress);
        }
        /*根据医院模糊查询*/
        if (!StringUtils.isEmpty(fromhospital)){
            queryWrapper.like("fromhospital",fromhospital);
        }
        queryWrapper.orderByDesc("id");//根据id倒序
        IPage<Address> page1 = iAddressService.page(page, queryWrapper);
        return R.SUCCESS(page1);
    }
    /*添加和修改，根据是否传入id，有则修改，无则添加*/
    @PostMapping("/save")
    public R save(@RequestBody Address address){
        if (iAddressService.saveOrUpdate(address)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
    /*删除*/
    @DeleteMapping("/del/{id}")
    public R del(@PathVariable Integer id){
        if (iAddressService.removeById(id)){
            return R.SUCCESS();
        }else {
            return R.FAIL();
        }
    }
}
